<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="SharedPreferneces,存储," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="SharedPreferneces是以键值对的形式进行保存的Android数据存储方式。">
<meta property="og:type" content="article">
<meta property="og:title" content="SharedPreferneces">
<meta property="og:url" content="http://yoursite.com/2017/03/02/201703/SharedPreferneces/index.html">
<meta property="og:site_name" content="HoneyGuide's Blog">
<meta property="og:description" content="SharedPreferneces是以键值对的形式进行保存的Android数据存储方式。">
<meta property="og:image" content="http://yoursite.com/img/201703/pfactivity.png">
<meta property="og:image" content="http://yoursite.com/img/201703/pfactivity2.png">
<meta property="og:updated_time" content="2017-03-02T02:15:15.783Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SharedPreferneces">
<meta name="twitter:description" content="SharedPreferneces是以键值对的形式进行保存的Android数据存储方式。">
<meta name="twitter:image" content="http://yoursite.com/img/201703/pfactivity.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'R1W2VDDO1V',
      apiKey: '773854c4c250e5a5cf9cddd97ebcc167',
      indexName: 'Hexo_search',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/03/02/201703/SharedPreferneces/"/>





  <title> SharedPreferneces | HoneyGuide's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner">﻿<div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">HoneyGuide's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Stay hungry & Stay young</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">

　　　　　　<!--增加的代码 start-->

          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/02/201703/SharedPreferneces/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HoneyGuide">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="HoneyGuide's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                SharedPreferneces
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-02T00:00:00+08:00">
                2017-03-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>SharedPreferneces是以键值对的形式进行保存的Android数据存储方式。<br><a id="more"></a></p>
<h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p>很多软件会有配置文件，里面存放这程序运行当中的各个属性值，由于其配置信息并不多，如果采用数据库来存放并不划算，因为数据库连接跟操作等耗时大大影响了程序的效率，因此我们使用键值这种一一对应的关系来存放这些配置信息。SharedPreferences正是Android中用于实现这中存储方式的技术。</p>
<p>SharedPreferences是以键值对的形式存储数据的，其使用非常简单，能够轻松的存放数据和读取数据。这种方式主要用来存储比较简单的一些数据，而且是标准的Boolean、Int、Float、Long、String等类型。</p>
<h3 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h3><p>android.content.SharedPreferences是一个接口，用来获取和修改持久化存储的数据。有三种获取系统中保存的持久化数据的方式：</p>
<ul>
<li>public SharedPreferences <strong>getPreferences</strong> (int mode)<br> 通过Activity对象获取，获取的是本Activity私有的Preference，保存在系统中的xml形式的文件的名称为这个Activity的名字，因此一个Activity只能有一个，属于这个Activity。</li>
<li>public SharedPreferences <strong>getSharedPreferences</strong>(String name, int mode)<br>  因为Activity继承了ContextWrapper，因此也是通过Activity对象获取，但是属于整个应用程序，可以有多个，以第一参数的name为文件名保存在系统中。</li>
<li>public static SharedPreferences <strong>getDefaultSharedPreferences</strong> (Context context)<br>PreferenceManager的静态函数，保存PreferenceActivity中的设置，属于整个应用程序，但是只有一个，Android会根据包名和PreferenceActivity的布局文件来起一个名字保存。</li>
</ul>
<p>通过以上方式取得SharedPreferences后就可以对数据进行读取或者保存了。</p>
<p>保存方式如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">String STORE_NAME = &quot;Settings&quot;;  </div><div class="line">SharedPreferences settings = getSharedPreferences(STORE_NAME, MODE_PRIVATE);  </div><div class="line">SharedPreferences.Editor editor = settings.edit();  </div><div class="line">editor.putInt(&quot;sourceType&quot;, 0);  </div><div class="line"> editor.commit();</div></pre></td></tr></table></figure></p>
<p>获得SharedPreferences，如果需要进行保存等修改操作，首先得通过其edit()方法获得SharedPreferences.Editor，然后就可以通过putInt、putString等方法以键值对(key-value)的方式保存数据，或者remove移除某个键(key)，及调用clear方法删除所有内容。最后需要调用commit方法是使修改生效。 </p>
<p>读取就更加简单了，只要获得SharedPreferences后，就可以通过getInt、getString等方法获取对应键(key)保存着的数据，如果没有找到key，则返回第二个参数作为默认值。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">SharedPreferences settings = getSharedPreferences(STORE_NAME, MODE_PRIVATE);  </div><div class="line"> int source = settings.getInt(&quot;sorceType&quot;, 1);</div></pre></td></tr></table></figure></p>
<p>数据的存入必须通过SharedPreferences对象的编辑器对象<strong>Editor</strong>来实现，存入（put）之后与写入数据库类似一定要<strong>commit</strong>。</p>
<h3 id="PreferencesActivity"><a href="#PreferencesActivity" class="headerlink" title="PreferencesActivity"></a>PreferencesActivity</h3><p>在Android开发过程中我们有很大的机会需要用到参数设置功能，那么在Android应用中，我们如何实现参数设置界面及参数存储呢，下面我们来介绍一下Android中的一个特殊Activity–PreferencesActivity。</p>
<p>PreferencesActivity是Android中专门用来实现程序设置界面及参数存储的一个Activity。</p>
<h4 id="PrefeneceActivity的基本组成"><a href="#PrefeneceActivity的基本组成" class="headerlink" title="PrefeneceActivity的基本组成"></a>PrefeneceActivity的基本组成</h4><p>PrefeneceActivity提供了几种元素可供使用。点击Add按钮，在打开的新窗口中可以看到以下几项：</p>
<p><img src="/img/201703/pfactivity.png" alt="image"></p>
<h4 id="获取ListPreference中选中的值"><a href="#获取ListPreference中选中的值" class="headerlink" title="获取ListPreference中选中的值"></a>获取ListPreference中选中的值</h4><p>接口：<strong>onSharedPreferenceChanged</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line">public class CallFireWall extends PreferenceActivity implements</div><div class="line">        OnSharedPreferenceChangeListener &#123;</div><div class="line">    ListPreference lp = null;</div><div class="line">    String whiteMode = &quot;1&quot;;</div><div class="line">    String blackMode = &quot;2&quot;;</div><div class="line"></div><div class="line">    /** Called when the activity is first created. */</div><div class="line">    @Override</div><div class="line">    public void onCreate(Bundle savedInstanceState) &#123;</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        addPreferencesFromResource(R.xml.preferences);</div><div class="line">        this.setTitle(&quot;Ying_er&quot; + &quot;  Call Safe&quot;);</div><div class="line">        SharedPreferences prefs = PreferenceManager</div><div class="line">                .getDefaultSharedPreferences(this);</div><div class="line">        prefs.registerOnSharedPreferenceChangeListener(this);</div><div class="line">        lp = (ListPreference) findPreference(getString(R.string.pref_mode_key));</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,</div><div class="line">            Preference preference) &#123;</div><div class="line">        // TODO Auto-generated method stub</div><div class="line">        return super.onPreferenceTreeClick(preferenceScreen, preference);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    @Override</div><div class="line">    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,</div><div class="line">            String key) &#123;</div><div class="line">        // TODO Auto-generated method stub</div><div class="line">        System.out.println(&quot;onSharedPreferenceChanged&quot;);</div><div class="line">        if (key == getString(R.string.pref_mode_key))&#123;</div><div class="line">            System.out.println(lp.getValue());</div><div class="line">            if(lp.getValue().equals(whiteMode))&#123;</div><div class="line">                </div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>注意：该事件需要注册。既：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">prefs.registerOnSharedPreferenceChangeListener(this);</div></pre></td></tr></table></figure></p>
<p>实际上，使用如下方法也是可以得到值的：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">PreferenceManager.getDefaultSharedPreferences(context).getBoolean(R.string.pref_mode_key, DEFAULT);</div></pre></td></tr></table></figure></p>
<h4 id="OnPreferenceChangeListener"><a href="#OnPreferenceChangeListener" class="headerlink" title="OnPreferenceChangeListener"></a>OnPreferenceChangeListener</h4><p>当PreferenceActivity中的内容改变时，Android系统会自动进行保存和持久化维护，我们只需要在要用的设置界面中数据的地方进行读取就可以了。同时Android还提供了OnPreferenceClickListener和OnPreferenceChangeListener两个与Preference相关的监听接口，当PreferenceActivity中的某一个Preference进行了点击或者改变的操作时，都会回调接口中的函数，这样可以第一个时间向其他Activity等通知系统设置进行了改变。</p>
<h4 id="OnPreferenceChangeListener与OnPreferenceClickListener的区别"><a href="#OnPreferenceChangeListener与OnPreferenceClickListener的区别" class="headerlink" title="OnPreferenceChangeListener与OnPreferenceClickListener的区别"></a>OnPreferenceChangeListener与OnPreferenceClickListener的区别</h4><p>OnPreferenceClickListener：是响应点击preference的事件，比如程序中，一点ListPreference，onPreferenceClick()中就弹出Toast。</p>
<p>OnPreferenceChangeListener：是响应preference的值被改变的事件（此时被改变的值需要自己存贮到SharedPreferences），比如程序中，点击ListPreference的某一项，onPreferenceChange()中就会弹出Toast。</p>
<p>当点击某个Preference时的调用流程是AdapterView.performItemClick－－&gt;PreferenceScreen.onItemClick－－&gt;Preference.performClick－－&gt;PreferenceActivity.onPreferenceTreeClick，当onPreferenceTreeClick返回true的时候就直接return了，没有走下面启动activity的地方了，因此要使一个Preference能够正常跳转到另外一个activity有两个条件，一是xml里面是否设置正确，第二是调用该xml的java类是否在onPreferenceTreeClick这个函数需要返回false。</p>
<p>PreferenceScreen本质上就是一个层次标签。<br>如果我们首选项的数目较少，但是我们依旧想为他们分组下，怎么办呢？<br>我们可以将上面代码中的嵌套PreferenceScreen改为PreferenceCategory。</p>
<p><img src="/img/201703/pfactivity2.png" alt="image"></p>
<h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><p><a href="http://blog.csdn.net/yuzhiboyi/article/details/7676293" target="_blank" rel="external">http://blog.csdn.net/yuzhiboyi/article/details/7676293</a><br><a href="https://www.oschina.net/question/157182_45602" target="_blank" rel="external">https://www.oschina.net/question/157182_45602</a><br><a href="http://blog.csdn.net/wujiangming/article/details/6218068" target="_blank" rel="external">http://blog.csdn.net/wujiangming/article/details/6218068</a><br><a href="http://blog.csdn.net/zhangqijie001/article/details/5838213/" target="_blank" rel="external">http://blog.csdn.net/zhangqijie001/article/details/5838213/</a><br><a href="http://blog.csdn.net/plussoft/article/details/9993759" target="_blank" rel="external">http://blog.csdn.net/plussoft/article/details/9993759</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/SharedPreferneces/" rel="tag"># SharedPreferneces</a>
          
            <a href="/tags/存储/" rel="tag"># 存储</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/02/201703/HttpURLConnection/" rel="next" title="HttpURLConnection">
                <i class="fa fa-chevron-left"></i> HttpURLConnection
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/03/02/201703/ThreadLocal/" rel="prev" title="ThreadLocal">
                ThreadLocal <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="HoneyGuide" />
          <p class="site-author-name" itemprop="name">HoneyGuide</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">34</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#方法"><span class="nav-number">2.</span> <span class="nav-text">方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PreferencesActivity"><span class="nav-number">3.</span> <span class="nav-text">PreferencesActivity</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#PrefeneceActivity的基本组成"><span class="nav-number">3.1.</span> <span class="nav-text">PrefeneceActivity的基本组成</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取ListPreference中选中的值"><span class="nav-number">3.2.</span> <span class="nav-text">获取ListPreference中选中的值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#OnPreferenceChangeListener"><span class="nav-number">3.3.</span> <span class="nav-text">OnPreferenceChangeListener</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#OnPreferenceChangeListener与OnPreferenceClickListener的区别"><span class="nav-number">3.4.</span> <span class="nav-text">OnPreferenceChangeListener与OnPreferenceClickListener的区别</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参考"><span class="nav-number">4.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HoneyGuide</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  



  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="local-search-pop-overlay">').css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

  


  

</body>
</html>
